var base_url;

$(document).ready(function() {
    base_url=$('#base_url').html();
    console.log("INFO: ************** SUS_DATOS CONTROLLER ****************");

    mostrarMontoTotalAPagar();
    setDataFiltrosSinFecha(localStorage.origen, localStorage.destino);
    var dataAccordion =createAccordionRow();
    $('#accordion1').html(dataAccordion);

    $('#form1_btn_atras').click(function() {
        history.go(-1);
    });

    $('#form1_btn_siguiente').click(function() {
        //if (true) {
        if (validateForm()) {
            saveDataSeleccionada();
            callUrlFromJS('online/confirmacion');
        }
    });

    $(".numDocumento").keypress(function (e) {
        if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)) return false;
    });
});

function saveDataSeleccionada() {
    var pasajeros = new Array();
    var i=0;
    $("#accordion1 input[type=text]").each(function(){
        pasajeros[i]=$.trim($(this).val());
        i++;
    });
    localStorage.pasajeros=JSON.stringify(pasajeros);
    localStorage.formaPago = $('#form1_forma_pago option:selected').text();
    localStorage.tarjeta = $('#form1_tarjeta option:selected').text();
}

function mostrarMontoTotalAPagar() {
    var asientos1= JSON.parse(localStorage.asientosPrimerPiso);
    var asientos2 = JSON.parse(localStorage.asientosSegundoPiso);
    var cant1=asientos1.length;
    var cant2=asientos2.length;
    var p1 =localStorage.precio1;
    var p2 =localStorage.precio2;
    var montoTotal= cant1*parseInt(p1.split(' ')[1]) + cant2*parseInt(p2.split(' ')[1]);
    localStorage.montoTotal = montoTotal;
    $('#form1_precio_total').text(montoTotal);
}


function validateForm() {
    var result=true;
    result=result && $('#form1_chbx_terminos').is(':checked');
    if (!result) {
        mostrarMensajeTemporal('#mensajeControl1', MENSAJE_ERROR_TERMINOS, 5000);
        return result;
    }
    $("#accordion1 input[type=text]").each(function(){
        if ($.trim($(this).val())==CADENA_VACIA){
            result=false;
        }
    });
    if (!result) {
        mostrarMensajeTemporal('#mensajeControl1', MENSAJE_ERROR_PASAJEROS, 5000);
        return result;
    }
    return result;
}


function createAccordionRow() {
    var asientos1= JSON.parse(localStorage.asientosPrimerPisoMostrar);
    var asientos2 = JSON.parse(localStorage.asientosSegundoPisoMostrar);
    var cad="";
    var cont;
    //alert(asientos1.length+" "+ asientos2.length);

    if (asientos1.length!=0){
        for (cont=1; cont<=asientos1.length; cont++) {
            cad+="<div class='accordion-group' style='background-color: #F2F4F4;'>";
            cad+="<div class='accordion-heading acordeon_head'>";
            cad+= "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion1' href='#collapse"+cont+"'>";
            cad+= "Precio "+localStorage.precio1+" | Pasajero Asiento "+ asientos1[cont-1]+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Click para comprimir/expandir)</a></div>";
            cad+="<div id='collapse"+cont+"' class='accordion-body collapse in'>";
            cad+="<div class='accordion-inner inner_datos'>";
            cad+="<div class='control-group group_datos'><label class='control-label label_datos'>Tipo Documento:</label>";
            cad+="<select>";
            cad+="<option value='1'>DNI</option><option value='2'>Pasaporte</option></select></div>";
            cad+= "<div class='control-group group_datos'><label class='control-label label_datos'>(*) Num. de Documento:</label>";
            cad+= "<input type='text' class='numDocumento' maxlength='11'></div>";
            cad+= "<div class='control-group group_datos'><label class='control-label label_datos'>(*) Nombres:</label>";
            cad+= "<input type='text' maxlength='25'></div>";
            cad+= "<div class='control-group group_datos'><label class='control-label label_datos'>(*) Apellidos:</label>";
            cad+= "<input type='text' maxlength='25'></div>";
            cad+="</div></div></div>"
        }
    }
    var collapse = cont+1;
    if (asientos2.length!=0){
        for (var cont2=1; cont2<=asientos2.length; cont2++) {
            cad+="<div class='accordion-group' style='background-color: #F2F4F4;'>";
            cad+="<div class='accordion-heading acordeon_head'>";
            cad+= "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion1' href='#collapse"+collapse+"'>";
            cad+= "Precio "+localStorage.precio2+" | Pasajero Asiento "+ asientos2[cont2-1]+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Click para comprimir/expandir)</a></div>";
            cad+="<div id='collapse"+collapse+"' class='accordion-body collapse in'>";
            cad+="<div class='accordion-inner inner_datos'>";
            cad+="<div class='control-group group_datos'><label class='control-label label_datos'>Tipo Documento:</label>";
            cad+="<select>";
            cad+="<option value='1'>DNI</option><option value='2'>Pasaporte</option></select></div>";
            cad+= "<div class='control-group group_datos'><label class='control-label label_datos'>(*) Num. de Documento:</label>";
            cad+= "<input type='text' class='numDocumento' maxlength='11'></div>";
            cad+= "<div class='control-group group_datos'><label class='control-label label_datos'>(*) Nombres:</label>";
            cad+= "<input type='text' maxlength='25'></div>";
            cad+= "<div class='control-group group_datos'><label class='control-label label_datos'>(*) Apellidos:</label>";
            cad+= "<input type='text' maxlength='25'></div>";
            cad+="</div></div></div>"
            collapse+=1;
        }
    }
    return cad;
}